//
//  SCButton.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/17.
//

#import "SCButton.h"
#import "SCBasicKit.h"

@interface SCButton ()

@property (nonatomic, assign) SCButtonCustomStyle customStyle;

@end

@implementation SCButton

- (instancetype)initWithClickCallBack:(void (^)(void))callback {
    if(self = [super init]) {
        [self addTarget:self action:[self sc_selecorBlock:^(id _Nonnull sender) {
            callback();
        }] forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (instancetype)initWithStyle:(SCButtonCustomStyle)style ClickCallBack:(void (^)(void))callback {
    
    if(self = [super init]) {
        [self addTarget:self action:[self sc_selecorBlock:^(id _Nonnull sender) {
            callback();
        }] forControlEvents:UIControlEventTouchUpInside];
        _customStyle = style;
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if(_customStyle) {
        switch (_customStyle) {
            case SCButtonCustomStyleBlueBorderRound:{
                self.layer.masksToBounds = true;
                self.layer.borderColor = sc_blueColor.CGColor;
                self.layer.cornerRadius = self.sc_height / 2;
                self.layer.borderWidth = 1;
                [self setTitleColor:sc_blueColor forState:UIControlStateNormal];
            }
                
                break;
            case SCButtonCustomStyleBlueFillRound:{
                self.layer.masksToBounds = true;
                self.backgroundColor = sc_blueColor;
                self.layer.cornerRadius = self.sc_height / 2;
                [self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            }
                break;
            case SCButtonCustomStyleBlueGradientRound:{
                self.layer.masksToBounds = true;
                self.layer.cornerRadius = self.sc_height / 2;
                [self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIImage *image = [UIImage sc_imageGradientStyleColors:@[[UIColor sc_colorWithHex:0x198bff],[UIColor sc_colorWithHex:0x28c1ff]] Position:SCGradientPositionLeftToRight Size:CGSizeMake(self.sc_width, self.sc_height)];
                    [self setBackgroundImage:image forState:UIControlStateNormal];
                });
            }
                
                break;
                
            default:
                break;
        }
        
        
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    [self setTitle:_title forState:UIControlStateNormal];
    self.titleLabel.font = sc_PingFangRegularFont(16);
}

@end
